package LibraryManagementSystem;

import java.util.ArrayList;
import java.util.Scanner;

public class Bookshelves{

    private final ArrayList<Book> lists = new ArrayList<>();
    private int sum = 0;

    //增加图书
    public boolean add (String bookName, String authorName, double price, String type, String state) {
        Book book = new Book(bookName, authorName, price, type, state);
        System.out.println("添加成功！");
        lists.add(book);
        sum += 1;
        return true;
    }

    //根据书名来删除图书
    public boolean remove (String bookName) {
        Book book = findBook(bookName);
        if (book == null) {
            System.out.println("删除失败！");
            return false;
        }
        System.out.println("确定要删除："+book.toString() +" 吗？ yes/no");
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        if (s.equals("no")) {
            System.out.println("删除失败！");
            return false;
        }
        lists.remove(book);
        System.out.println("删除成功！");
        sum -= 1;
        return true;
    }

    //根据图书名查找图书相关信息
    private Book findBook (String bookName) {
        for (Book book : lists) {
            if (book.getBookName() == bookName) {
                return book;
            }
        }
        return null;
    }

    //拿到的书进行展示
    public void find (String bookName) {
        Book book = findBook(bookName);
        if (book == null) {
            System.out.println("找不到该书籍！");
        }else {
            System.out.println("找到该书籍啦~");
            System.out.println(book.toString());
        }
    }

    //对图书进行修改
    public boolean modify (String bookName1) {

        //根据图书名找到书籍
        Book f = findBook(bookName1);
        if (f == null) {
            System.out.println("不存在该书籍，修改失败！");
            return false;
        }else {
            Scanner scanner = new Scanner(System.in);
            System.out.println("开始修改：");
            System.out.print("请输入书名：");
            String bookName = scanner.next();

            System.out.println();
            System.out.print("请输入作者名：");
            String authorName = scanner.next();

            System.out.println();
            System.out.print("请输入书本价格：");
            double price = scanner.nextDouble();

            System.out.println();
            System.out.print("请输入书的类型：");
            String type = scanner.next();

            System.out.println();
            System.out.print("请输入书本目前状态：");
            String state = scanner.next();

            f.setBookName(bookName);
            f.setAuthorName(authorName);
            f.setPrice(price);
            f.setType(type);
            f.setState(state);

            System.out.println("修改成功！");
            return true;
        }
    }

    //对所有的书进行展示
    public void display () {
        for (Book b : lists) {
            System.out.println(b);
        }
    }


    //退出系统
    public void exit () {
        System.exit(0);
    }

    //查看当前的书数量
    public int sum () {
        return sum;
    }

    //未借阅的书的数量
    public int Borrowing () {
        int b = 0;
        for (Book book : lists) {
            if (book.getState().equals("未借出")) {
                b++;
            }
        }
        return b;
    }

    @Override
    public String toString() {
        return "Bookshelves{" +
                "lists=" + lists +
                '}';
    }
}
